package com.dev.listener;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/myServlet03")
public class MyServlet extends HttpServlet {
    
    @Override
    public void service(HttpServletRequest req, HttpServletResponse res) throws IOException {
        // 获取或创建session
        HttpSession session = req.getSession();
        
        String action = req.getParameter("action");
        if ("login".equals(action)) {
            // 登录
            session.setAttribute("user", "admin");
        } else if ("logout".equals(action)) {
            // 登出 销毁session
            session.invalidate();
            res.getWriter().write("<h1>退出成功</h1>");
            return;
        }
        
        res.setContentType("text/html;charset=UTF-8");
        // res.getWriter().write("<h1>Hello World</h1>");
        res.getWriter().write("<h3><a href='myServlet03?action=logout'>退出</a></h3>");
    }
}
